package bb.lanxing.manager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.garmin.fit.MonitoringReader;
import com.github.mikephil.charting.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import bb.lanxing.App;
import bb.lanxing.calc.data.DisplayPoint;

public class TWatchManager {
    public static final String TAG = "twatch";
    private static final String WATCH_RECEIVER_PKG_NAME = "bb.lanxing.twatch";
    public static final String WORKOUT_CYCLING = "cycling";
    public static final String WORKOUT_RUNNING = "running";
    public static final String WORKOUT_START = "start";
    public static final String WORKOUT_STOP = "stop";
    public static final String WORKOUT_WALKING = "walking";
    private static TWatchManager instance = new TWatchManager();
    private Context context = null;
    private boolean isWatchConnect = true;
    private int sendFailedCount = 0;
    private BroadcastReceiver mReceiverBluetooth = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("Action.Tws.device_connected".equals(action)) {
                Log.d(TWatchManager.TAG, "onReceive action : Action.Tws.device_connected");
                App.getContext().showMessage("腾讯手表已连接");
                TWatchManager.this.isWatchConnect = true;
            }
            if ("Action.Tws.device_active_disconnected".equals(action)) {
                Log.d(TWatchManager.TAG, "onReceive action : Action.Tws.device_active_disconnected");
                TWatchManager.this.isWatchConnect = false;
            }
            if ("Action.Tws.device_passive_disconnected".equals(action)) {
                Log.d(TWatchManager.TAG, "onReceive action : Action.Tws.device_passive_disconnected");
                App.getContext().showMessage("腾讯手表已断开");
                TWatchManager.this.isWatchConnect = false;
            }
        }
    };

    private void regDataRecver() {
    }

    private void registerBluetoothDevice() {
    }

    private void unRegDataRecver() {
    }

    public void sendData(String str) {
    }

    private TWatchManager() {
    }

    public static TWatchManager getInstance() {
        return instance;
    }

    public void sendWorkout(DisplayPoint displayPoint, int i) {
        JSONObject jSONObject = new JSONObject();
        if (displayPoint != null) {
            try {
                jSONObject.put(MonitoringReader.DISTANCE_STRING, displayPoint.getDistance(Utils.DOUBLE_EPSILON));
                jSONObject.put("speed", displayPoint.getSpeed(Utils.DOUBLE_EPSILON));
                jSONObject.put("duration", displayPoint.getDuration(0L));
                jSONObject.put("maxSpeed", displayPoint.getMaxSpeed(Utils.DOUBLE_EPSILON));
                jSONObject.put("elevationGain", (int) displayPoint.getElevationGain(0.0f));
                jSONObject.put("avgCadence", displayPoint.getAvgCadence(0));
                jSONObject.put("maxCadence", displayPoint.getMaxCadence(0));
                jSONObject.put("avgHeartRate", displayPoint.getAvgHeartrate(0));
                jSONObject.put("maxHeartRate", displayPoint.getMaxHeartrate(0));
                jSONObject.put("type", displayPoint.getSportType());
            } catch (JSONException e) {
                e.printStackTrace();
                return;
            }
        }
        try {
            jSONObject.put("state", i);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        sendData(jSONObject.toString());
    }

    public void init(Context context) {
        this.context = context;
        registerBluetoothDevice();
        regDataRecver();
        Log.d(TAG, "init TWatchManager success ");
    }

    public void close() {
        unRegDataRecver();
    }

    public boolean isWatchConnect() {
        return this.isWatchConnect;
    }
}
